<?php
/**
* User Prices Controller
*
* This handles all AJAX requests from the User GUI.
* These methods will create an AJAX response containing JSON data. The JSON
* data is read by the JS code and rendered into interface.
*
* @access 	public
* @author 	Rob, Valentin
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/api
* @since  	11-2010
*/
class RM_User_ExcursionPricesController extends RM_Controller
{

    /**
     * gets prices from for a specified unit.
     *
     * @param   request id  unit id
     * @return 	json    json array information in format:
     */
    function getpricesfromAction(){
        $uid = $this->_getParam('id');
        $unitsDAO = new RM_Units();
        
        $taxSystem = RM_Environment::getInstance()->getTaxSystem();
        $tax = 0;

        $unitObj = $unitsDAO->get($uid);

        $stardateObj = new RM_Date(strtotime(date("Y-m-d"))); // start date of now

        // get the last price date entered....
        $unitPrices = new RM_UnitExcursionPrices;
        $end_datetime = $unitPrices->getLastPriceBandDate($uid);

        $enddateObj = new RM_Date(strtotime($end_datetime));

        $periodObj = new RM_Reservation_Period($stardateObj , $enddateObj);

        $persons = new RM_Reservation_Persons(array("adults"=>1,"children"=>0,"infants"=>0));

        $information = new RM_Prices_Information($unitObj, $periodObj, $persons);

        $priceSystem = RM_Environment::getInstance()->getPriceSystem();
        $subtotal = $priceSystem->getLowestUnitPrice($information);

        // tax to be added...
        $tax = $taxSystem->calculateTotalTax($unitObj, $subtotal);

        $total = $subtotal + $tax;       
    }

    function listallAction(){
        ob_clean();
        $uid = $this->_getParam('uid');

        $unitModel = new RM_Units();
        $language = RM_Environment::getInstance()->getLocale();
        $unit = $unitModel->get($uid, $language);
        
        $priceObj = new RM_UnitExcursionPrices;
        $prices = $priceObj->getCurrent($unit)->toArray();
        $this->view->unit = $unit;
        $this->view->prices = $prices;

        echo $this->view->render('ExcursionPrices/listall.phtml');
        die();
    }

    function datepickerJsAction(){
        return "var RM_ExcursionPrices_DatepickerWindow = {width: 400, height: 340}; ";
    }

    function datepickerAction(){
        ob_clean();
        $unitID = $this->_getParam('unit_id', null);
        $unitModel = new RM_Units();
        $unit = $unitModel->get($unitID);
        $reservationModel = new RM_Reservations();
        $reservations = $reservationModel->fetchAllForUnitCalendar($unit);

        $jsonDisabledPeriods = array();
        $config = new RM_Config();
        foreach ($reservations as $period) {
            $jsonPeriod = new stdClass;
            $jsonPeriod->start = $config->convertDates($period->start_datetime, RM_Config::JS_DATEFORMAT, RM_Config::PHP_DATEFORMAT);
            $jsonPeriod->end = $config->convertDates($period->end_datetime, RM_Config::JS_DATEFORMAT, RM_Config::PHP_DATEFORMAT);
            $jsonDisabledPeriods[] = clone $jsonPeriod;
        }

        // add the disabled days to the calendar

        $configObject = new RM_UnitExcursionPricesConfig();
        $disabledDays = array();
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "monday_allowed") == "0" ? 1 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "tuesday_allowed") == "0" ? 2 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "wednesday_allowed") == "0" ? 3 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "thursday_allowed") == "0" ? 4 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "friday_allowed") == "0" ? 5 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "saturday_allowed") == "0" ? 6 : null);
        $disabledDays[] = ($configObject->fetchValueByUnit($unitID, "sunday_allowed") == "0" ? 0 : null);

        $json = Zend_Json::encode($jsonDisabledPeriods);
        $this->view->calendardata = $json;
        $this->view->disabledDays = implode(",", $disabledDays);
        $this->view->unit = $unit;
        echo $this->view->render('ExcursionPrices/datepicker.phtml');
        die();
    }

    function getpersonsJsonAction(){
        $hositality = new RM_Module_ExcursionPrices();
        $data = $hositality->getGlobalPeopleMaximums();
        return array(
            'data' => $data
        );
    }

    /*
     * saves the board type to the session for the calculator to use.
     */
    function setdatecriteriaJsonAction(){

        $criteria = RM_Reservation_Manager::getInstance()->getCriteria();
        $criteria->adults = $this->_getParam('adults', 1);
        $criteria->children = $this->_getParam('children', 0);
        $criteria->infants = $this->_getParam('infants', 0);
        RM_Reservation_Manager::getInstance()->resetCriteria()->setCriteria($criteria);

        return array(
            'data' => 'saved'
        );
    }
}